﻿using System;
using System.Text;
using TestEFAbstractEntity.Entities;

namespace TestEFAbstractEntity1
{
    public static class WorkNodeExtension
    {
        public static void RenderToHtml(this WorkNode workNode, StringBuilder sbHtml)
        {
            Console.WriteLine($"节点【{workNode.Name}】，下个节点【{workNode.NextNode?.Name}】");
            if (workNode == null) return;
            if (workNode is SimpleWorkNode)
            {
                (workNode as SimpleWorkNode).RenderToHtml(sbHtml);
            }
            else if (workNode is CountersignNode)
            {
                (workNode as CountersignNode).RenderToHtml(sbHtml);
            }
            else if (workNode is ParalleNode)
            {
                (workNode as ParalleNode).RenderToHtml(sbHtml);
            }
            else
            {
                throw new Exception($"未知工作节点【{workNode.GetType()}】");
            }
            var nextNode = workNode.NextNode;
            if (nextNode != null)
            {
                sbHtml.AppendLine("<div class='node-next'>");
                sbHtml.AppendLine(" =&gt; ");
                sbHtml.AppendLine("</div>");

                nextNode.RenderToHtml(sbHtml);

                nextNode = nextNode.NextNode;
            }
        }

        private static void RenderToHtml(this SimpleWorkNode simpleWorkNode, StringBuilder sbHtml)
        {
            sbHtml.AppendLine("<div class='simple-work-node'>");
            sbHtml.AppendLine("     <div class='title simple-work-node-title'>");
            sbHtml.AppendLine($"         <span>{simpleWorkNode.Name}</span>");
            sbHtml.AppendLine("     </div>");
            sbHtml.AppendLine("</div>");
        }
        private static void RenderToHtml(this CountersignNode countersignNode, StringBuilder sbHtml)
        {
            sbHtml.AppendLine("<div class='countersign-work-node'>");
            sbHtml.AppendLine("     <div class='title countersign-work-node-title'>");
            sbHtml.AppendLine($"         <span>{countersignNode.Name}</span>");
            sbHtml.AppendLine("     </div>");
            sbHtml.AppendLine("     <div class='child'>");
            foreach(var childNode in countersignNode.ChildNodes)
            {
                sbHtml.AppendLine("<div class='child-item'>");
                childNode.RenderToHtml(sbHtml);
                sbHtml.AppendLine("</div>");
            }
            sbHtml.AppendLine("     </div>");
            sbHtml.AppendLine("</div>");
        }
        private static void RenderToHtml(this ParalleNode paralleNode, StringBuilder sbHtml)
        {
            sbHtml.AppendLine("<div class='paralle-work-node'>");
            sbHtml.AppendLine("     <div class='title paralle-work-node-title'>");
            sbHtml.AppendLine($"         <span>{paralleNode.Name}</span>");
            sbHtml.AppendLine("     </div>");
            sbHtml.AppendLine("     <div class='child'>");
            foreach (var childNode in paralleNode.ChildNodes)
            {
                sbHtml.AppendLine("<div class='child-item'>");
                childNode.RenderToHtml(sbHtml);
                sbHtml.AppendLine("</div>");
            }
            sbHtml.AppendLine("     </div>");
            sbHtml.AppendLine("</div>");
        }
    }
}
